// Attach this script to a camera and it will
// draw a debug line triangle triangle
// at the triangle where you place the mouse.
function Update () {
    // Only if we hit something, do we continue
    var hitme : RaycastHit;
    

	if (!Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), hitme))
        return;

    // Just in case, also make sure the collider also has a renderer
    // material and texture
    var meshCollider = hitme.collider as MeshCollider;
    if (meshCollider == null || meshCollider.sharedMesh == null)
        return;

    var mesh : Mesh = meshCollider.sharedMesh;
    mesh.RecalculateNormals();
    var vertices = mesh.vertices;
    var triangles = mesh.triangles;
	var normals = mesh.normals;
	


    // Extract local space vertices that were hit
    var p0 = vertices[triangles[hitme.triangleIndex * 3 + 0]];
    var p1 = vertices[triangles[hitme.triangleIndex * 3 + 1]];    
    var p2 = vertices[triangles[hitme.triangleIndex * 3 + 2]];   


	var n0 = normals[triangles[hitme.triangleIndex * 3 + 0]];
	


    // Transform local space vertices to world space
    var hitTransform : Transform = hitme.collider.transform;
    p0 = hitTransform.TransformPoint(p0);
    p1 = hitTransform.TransformPoint(p1);
    p2 = hitTransform.TransformPoint(p2);
    // Display with Debug.DrawLine
    Debug.DrawLine(p0, p1);
    Debug.DrawLine(p1, p2);
    Debug.DrawLine(p2, p0);
    
    Debug.DrawLine(hitme.point, hitme.normal, Color.yellow);
}
